package com.rzico.sbl.ui

import android.widget.ImageView
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.MessageType
import com.rzico.sbl.viewmodel.MessageViewMode
import com.tim.library.MessageNotification
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.gone
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.helper.TimeHelper
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common.extend.notEmpty
import com.xinnuo.common.helper.longValue
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.view.BadgeTextView
import com.xinnuo.slimadapter.SlimAdapter

class MessageActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<MessageViewMode>()

    override fun initData() {
        initTitle(title = "我的消息")

        initLayout()

        MessageNotification
            .getInstance(getIContext())
            .clearAll()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData()
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关消息记录！"
                }

                recycleList.also { layout ->
                    layout.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData() }
                    )

                    layout.addItemDecoration(SpaceTBDecoration())

                    mAdapter = SlimAdapter.creator()
                        .register<MessageType>(R.layout.item_msg_list) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_msg_name, bean.name)
                                text(
                                    R.id.item_msg_time,
                                    TimeHelper.getDiffTime(bean.create_date.longValue)
                                )
                                text(R.id.item_msg_content, bean.content)

                                with<BadgeTextView>(R.id.item_msg_hint) {
                                    it.setBadgeCount(bean.num)
                                }

                                with<ImageView>(R.id.item_msg_img) {
                                    when (bean.type) {
                                        "0" -> it.setImageResource(R.mipmap.icon_msg04)
                                        "1" -> it.setImageResource(R.mipmap.icon_msg02)
                                        "2" -> it.setImageResource(R.mipmap.icon_msg01)
                                        "3" -> it.setImageResource(R.mipmap.icon_msg06)
                                        "4" -> it.setImageResource(R.mipmap.icon_msg07)
                                        "5" -> it.setImageResource(R.mipmap.icon_msg08)
                                        "6" -> it.setImageResource(R.mipmap.icon_msg09)
                                        "7" -> it.setImageResource(R.mipmap.icon_msg10)
                                        "8" -> it.setImageResource(R.mipmap.icon_msg11)
                                        "9" -> it.setImageResource(R.mipmap.icon_msg12)
                                        "10" -> it.setImageResource(R.mipmap.icon_msg13)
                                        "11" -> it.setImageResource(R.mipmap.icon_msg14)
                                        "12" -> it.setImageResource(R.mipmap.icon_msg15)
                                        "13" -> it.setImageResource(R.mipmap.icon_msg03)
                                        "14" -> it.setImageResource(R.mipmap.icon_msg16)
                                        "15" -> it.setImageResource(R.mipmap.icon_msg17)
                                    }
                                }

                                clicked {
                                    startActivityEx<MessageSecondActivity>(
                                        "title" to bean.name,
                                        "type" to bean.type,
                                        "num" to bean.num
                                    )
                                }
                            }
                        }
                        .attachTo(layout)
                }
            }
    }

    override fun onRestart() {
        super.onRestart()

        updateList()
    }

    /**
     * 消息类型列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .messageSummary(
                onSuccess = {
                    mList.clear()
                    mList.addItems(it)

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )

    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        mList.notEmpty {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData()
    }

}